const int BUFFER_SIZE = 1000;

class CRegistry
{

public:
	/**
	 * @brief CRegistry
	 *
	 * CRegistry::CRegistry
	 *
	 * @return
	 */
	CRegistry()
	{
		m_hKey = NULL;
	}

	/**
	 * @brief ~CRegistry
	 *
	 * CRegistry::~CRegistry
	 *
	 * @return
	 */
	virtual ~CRegistry()
	{
		Close();
	}

	/**
	 * @brief Open
	 *
	 * CRegistry::Open
	 *
	 * @param HKEY hKey
	 * @param LPCTSTR strKey
	 * @return BOOL
	 */
	BOOL Open(HKEY hKey, LPCTSTR strKey)
	{
		DWORD dw;
		return ::RegCreateKeyEx(
						 hKey,
						 strKey,
						 0,
						 _T("Thinking Thing Software"),
						 REG_OPTION_NON_VOLATILE,
						 KEY_ALL_ACCESS,
						 NULL,
						 &m_hKey,
						 &dw) == ERROR_SUCCESS;
	}

	/**
	 * @brief Close
	 *
	 * CRegistry::Close
	 *
	 * @return BOOL
	 */
	BOOL Close()
	{
		if (m_hKey != NULL)
		{
			BOOL Result = ::RegCloseKey(m_hKey) == ERROR_SUCCESS;
			m_hKey = NULL;

			return Result;
		}
		else
			return TRUE;
	}

	/**
	 * @brief GetDWORD
	 *
	 * CRegistry::GetDWORD
	 *
	 * @param LPTSTR szValue
	 * @param DWORD dwDefault
	 * @return DWORD
	 */
	DWORD GetDWORD(LPTSTR szValue, DWORD dwDefault = 0)
	{
		DWORD dwType, dwData;
		DWORD dwSize = 4;

		if (::RegQueryValueEx(m_hKey, szValue,
													NULL, &dwType,
													(BYTE*)&dwData, &dwSize) != ERROR_SUCCESS)
			return dwDefault;

		if (dwType != REG_DWORD)
			return dwDefault;

		return dwData;
	}

	/**
	 * @brief SetString
	 *
	 * CRegistry::SetString
	 *
	 * @param LPCTSTR szValue
	 * @param LPTSTR szData
	 * @return BOOL
	 */
	BOOL SetString(LPCTSTR szValue, LPTSTR szData)
	{
		return RegSetValueEx(m_hKey, szValue, 0, REG_SZ, (CONST BYTE *)szData, lstrlen(szData) + 1) == ERROR_SUCCESS;
	}

	/**
	 * @brief SetDWORD
	 *
	 * CRegistry::SetDWORD
	 *
	 * @param LPCTSTR szValue
	 * @param DWORD dwData
	 * @return BOOL
	 */
	BOOL SetDWORD(LPCTSTR szValue, DWORD dwData)
	{
		return RegSetValueEx(m_hKey, szValue, 0, REG_DWORD, (CONST BYTE *)&dwData, sizeof(DWORD)) == ERROR_SUCCESS;
	}

public:
	//
	HKEY m_hKey;
};